{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a988f665",
   "metadata": {},
   "source": [
    "# Applying Stops\n",
    "\n",
    "Stops are used to automatically buy or sell a security once it reaches a specified price level. They can be useful for limiting potential losses by allowing traders to exit bad trades automatically, as well as for taking profits by selling a security automatically when it reaches a certain price level.\n",
    "\n",
    "**PyBroker** supports the simulation of stops, which is explained in detail in this notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9511bc55",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pybroker\n",
    "from pybroker import Strategy, YFinance\n",
    "\n",
    "pybroker.enable_data_source_cache('stops')\n",
    "\n",
    "strategy = Strategy(YFinance(), '1/1/2018', '1/1/2023')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18b8317b",
   "metadata": {},
   "source": [
    "## Stop Loss\n",
    "\n",
    "A stop loss order is used to automatically exit a trade once the security's price reaches or falls below a specified level. For example, the following code shows an example of a stop loss order set at ``20%`` below the entry price:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dc2b4f7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2018-01-01 00:00:00 to 2023-01-01 00:00:00\n",
      "\n",
      "Loading bar data...\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "Loaded bar data: 0:00:00 \n",
      "\n",
      "Test split: 2018-01-02 00:00:00 to 2022-12-30 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:02\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>21.36</td>\n",
       "      <td>17.09</td>\n",
       "      <td>4679</td>\n",
       "      <td>-19988.69</td>\n",
       "      <td>-20.00</td>\n",
       "      <td>-19988.69</td>\n",
       "      <td>58</td>\n",
       "      <td>-344.63</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-03-29</td>\n",
       "      <td>2019-05-20</td>\n",
       "      <td>17.31</td>\n",
       "      <td>13.73</td>\n",
       "      <td>4622</td>\n",
       "      <td>-16531.36</td>\n",
       "      <td>-20.66</td>\n",
       "      <td>-36520.04</td>\n",
       "      <td>286</td>\n",
       "      <td>-57.80</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    type symbol entry_date  exit_date  entry   exit  shares       pnl  \\\n",
       "id                                                                      \n",
       "1   long   TSLA 2018-01-03 2018-03-28  21.36  17.09    4679 -19988.69   \n",
       "2   long   TSLA 2018-03-29 2019-05-20  17.31  13.73    4622 -16531.36   \n",
       "\n",
       "    return_pct   agg_pnl  bars  pnl_per_bar  stop  \n",
       "id                                                 \n",
       "1       -20.00 -19988.69    58      -344.63  loss  \n",
       "2       -20.66 -36520.04   286       -57.80  loss  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def buy_with_stop_loss(ctx):\n",
    "    if not ctx.long_pos():\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)\n",
    "        ctx.stop_loss_pct = 20\n",
    "        \n",
    "strategy.add_execution(buy_with_stop_loss, ['TSLA'])\n",
    "result = strategy.backtest()\n",
    "result.trades"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c47bdaf8",
   "metadata": {},
   "source": [
    "## Take Profit\n",
    "\n",
    "Similarly, a take profit order can be used to lock in profits on a trade. The following code adds a take profit order at ``10%`` above the entry price:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e9eecfd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2018-01-01 00:00:00 to 2023-01-01 00:00:00\n",
      "\n",
      "Loaded cached bar data.\n",
      "\n",
      "Test split: 2018-01-02 00:00:00 to 2022-12-30 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:00\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2018-01-22</td>\n",
       "      <td>21.36</td>\n",
       "      <td>23.50</td>\n",
       "      <td>4679</td>\n",
       "      <td>9994.34</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9994.34</td>\n",
       "      <td>12</td>\n",
       "      <td>832.86</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-23</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>23.72</td>\n",
       "      <td>18.98</td>\n",
       "      <td>4637</td>\n",
       "      <td>-21997.93</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>-12003.58</td>\n",
       "      <td>44</td>\n",
       "      <td>-499.95</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>2018-04-04</td>\n",
       "      <td>17.36</td>\n",
       "      <td>19.10</td>\n",
       "      <td>4727</td>\n",
       "      <td>8206.07</td>\n",
       "      <td>10.0</td>\n",
       "      <td>-3797.51</td>\n",
       "      <td>4</td>\n",
       "      <td>2051.52</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-04-05</td>\n",
       "      <td>2018-06-07</td>\n",
       "      <td>19.82</td>\n",
       "      <td>21.80</td>\n",
       "      <td>4853</td>\n",
       "      <td>9618.65</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5821.13</td>\n",
       "      <td>44</td>\n",
       "      <td>218.61</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-06-08</td>\n",
       "      <td>2018-06-12</td>\n",
       "      <td>21.39</td>\n",
       "      <td>23.53</td>\n",
       "      <td>4947</td>\n",
       "      <td>10581.63</td>\n",
       "      <td>10.0</td>\n",
       "      <td>16402.77</td>\n",
       "      <td>2</td>\n",
       "      <td>5290.82</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-07-29</td>\n",
       "      <td>2022-10-07</td>\n",
       "      <td>288.71</td>\n",
       "      <td>230.97</td>\n",
       "      <td>1010</td>\n",
       "      <td>-58319.42</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>133480.29</td>\n",
       "      <td>49</td>\n",
       "      <td>-1190.19</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-10-10</td>\n",
       "      <td>2022-11-09</td>\n",
       "      <td>222.68</td>\n",
       "      <td>178.14</td>\n",
       "      <td>1046</td>\n",
       "      <td>-46584.66</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>86895.63</td>\n",
       "      <td>22</td>\n",
       "      <td>-2117.48</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-11-10</td>\n",
       "      <td>2022-12-19</td>\n",
       "      <td>185.51</td>\n",
       "      <td>148.41</td>\n",
       "      <td>1007</td>\n",
       "      <td>-37361.71</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>49533.92</td>\n",
       "      <td>26</td>\n",
       "      <td>-1436.99</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-12-20</td>\n",
       "      <td>2022-12-27</td>\n",
       "      <td>143.07</td>\n",
       "      <td>114.46</td>\n",
       "      <td>997</td>\n",
       "      <td>-28528.16</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>21005.76</td>\n",
       "      <td>4</td>\n",
       "      <td>-7132.04</td>\n",
       "      <td>loss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>89</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-12-28</td>\n",
       "      <td>2022-12-29</td>\n",
       "      <td>112.25</td>\n",
       "      <td>123.48</td>\n",
       "      <td>1078</td>\n",
       "      <td>12100.55</td>\n",
       "      <td>10.0</td>\n",
       "      <td>33106.31</td>\n",
       "      <td>1</td>\n",
       "      <td>12100.55</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>89 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    type symbol entry_date  exit_date   entry    exit  shares       pnl  \\\n",
       "id                                                                        \n",
       "1   long   TSLA 2018-01-03 2018-01-22   21.36   23.50    4679   9994.34   \n",
       "2   long   TSLA 2018-01-23 2018-03-27   23.72   18.98    4637 -21997.93   \n",
       "3   long   TSLA 2018-03-28 2018-04-04   17.36   19.10    4727   8206.07   \n",
       "4   long   TSLA 2018-04-05 2018-06-07   19.82   21.80    4853   9618.65   \n",
       "5   long   TSLA 2018-06-08 2018-06-12   21.39   23.53    4947  10581.63   \n",
       "..   ...    ...        ...        ...     ...     ...     ...       ...   \n",
       "85  long   TSLA 2022-07-29 2022-10-07  288.71  230.97    1010 -58319.42   \n",
       "86  long   TSLA 2022-10-10 2022-11-09  222.68  178.14    1046 -46584.66   \n",
       "87  long   TSLA 2022-11-10 2022-12-19  185.51  148.41    1007 -37361.71   \n",
       "88  long   TSLA 2022-12-20 2022-12-27  143.07  114.46     997 -28528.16   \n",
       "89  long   TSLA 2022-12-28 2022-12-29  112.25  123.48    1078  12100.55   \n",
       "\n",
       "    return_pct    agg_pnl  bars  pnl_per_bar    stop  \n",
       "id                                                    \n",
       "1         10.0    9994.34    12       832.86  profit  \n",
       "2        -20.0  -12003.58    44      -499.95    loss  \n",
       "3         10.0   -3797.51     4      2051.52  profit  \n",
       "4         10.0    5821.13    44       218.61  profit  \n",
       "5         10.0   16402.77     2      5290.82  profit  \n",
       "..         ...        ...   ...          ...     ...  \n",
       "85       -20.0  133480.29    49     -1190.19    loss  \n",
       "86       -20.0   86895.63    22     -2117.48    loss  \n",
       "87       -20.0   49533.92    26     -1436.99    loss  \n",
       "88       -20.0   21005.76     4     -7132.04    loss  \n",
       "89        10.0   33106.31     1     12100.55  profit  \n",
       "\n",
       "[89 rows x 13 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def buy_with_stop_loss_and_profit(ctx):\n",
    "    if not ctx.long_pos():\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)\n",
    "        ctx.stop_loss_pct = 20\n",
    "        ctx.stop_profit_pct = 10\n",
    "        \n",
    "strategy.clear_executions()\n",
    "strategy.add_execution(buy_with_stop_loss_and_profit, ['TSLA'])\n",
    "result = strategy.backtest()\n",
    "result.trades"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ad30dea",
   "metadata": {},
   "source": [
    "## Trailing Stop\n",
    "\n",
    "A trailing stop is an order that is used to exit a trade once the instrument's price falls a certain percentage or cash amount below its highest market price. Here is an example of setting a trailing stop at ``20%`` below the highest market price:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "28da1b1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2018-01-01 00:00:00 to 2023-01-01 00:00:00\n",
      "\n",
      "Loaded cached bar data.\n",
      "\n",
      "Test split: 2018-01-02 00:00:00 to 2022-12-30 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:00\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2018-01-22</td>\n",
       "      <td>21.36</td>\n",
       "      <td>23.50</td>\n",
       "      <td>4679</td>\n",
       "      <td>9994.34</td>\n",
       "      <td>10.00</td>\n",
       "      <td>9994.34</td>\n",
       "      <td>12</td>\n",
       "      <td>832.86</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-23</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>23.72</td>\n",
       "      <td>19.20</td>\n",
       "      <td>4637</td>\n",
       "      <td>-20961.72</td>\n",
       "      <td>-19.06</td>\n",
       "      <td>-10967.37</td>\n",
       "      <td>44</td>\n",
       "      <td>-476.40</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>2018-04-04</td>\n",
       "      <td>17.36</td>\n",
       "      <td>19.10</td>\n",
       "      <td>4783</td>\n",
       "      <td>8303.29</td>\n",
       "      <td>10.00</td>\n",
       "      <td>-2664.08</td>\n",
       "      <td>4</td>\n",
       "      <td>2075.82</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-04-05</td>\n",
       "      <td>2018-06-07</td>\n",
       "      <td>19.82</td>\n",
       "      <td>21.80</td>\n",
       "      <td>4910</td>\n",
       "      <td>9731.62</td>\n",
       "      <td>10.00</td>\n",
       "      <td>7067.54</td>\n",
       "      <td>44</td>\n",
       "      <td>221.17</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-06-08</td>\n",
       "      <td>2018-06-12</td>\n",
       "      <td>21.39</td>\n",
       "      <td>23.53</td>\n",
       "      <td>5005</td>\n",
       "      <td>10705.70</td>\n",
       "      <td>10.00</td>\n",
       "      <td>17773.23</td>\n",
       "      <td>2</td>\n",
       "      <td>5352.85</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-08-02</td>\n",
       "      <td>2022-10-03</td>\n",
       "      <td>300.25</td>\n",
       "      <td>251.73</td>\n",
       "      <td>1095</td>\n",
       "      <td>-53125.76</td>\n",
       "      <td>-16.16</td>\n",
       "      <td>175768.16</td>\n",
       "      <td>43</td>\n",
       "      <td>-1235.48</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-10-04</td>\n",
       "      <td>2022-10-24</td>\n",
       "      <td>249.75</td>\n",
       "      <td>199.80</td>\n",
       "      <td>1104</td>\n",
       "      <td>-55144.80</td>\n",
       "      <td>-20.00</td>\n",
       "      <td>120623.36</td>\n",
       "      <td>14</td>\n",
       "      <td>-3938.91</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-10-25</td>\n",
       "      <td>2022-11-08</td>\n",
       "      <td>217.18</td>\n",
       "      <td>189.92</td>\n",
       "      <td>1015</td>\n",
       "      <td>-27668.90</td>\n",
       "      <td>-12.55</td>\n",
       "      <td>92954.46</td>\n",
       "      <td>10</td>\n",
       "      <td>-2766.89</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>105</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-11-09</td>\n",
       "      <td>2022-12-13</td>\n",
       "      <td>186.50</td>\n",
       "      <td>160.66</td>\n",
       "      <td>1008</td>\n",
       "      <td>-26050.75</td>\n",
       "      <td>-13.86</td>\n",
       "      <td>66903.71</td>\n",
       "      <td>23</td>\n",
       "      <td>-1132.64</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2022-12-14</td>\n",
       "      <td>2022-12-22</td>\n",
       "      <td>158.46</td>\n",
       "      <td>128.79</td>\n",
       "      <td>1036</td>\n",
       "      <td>-30736.04</td>\n",
       "      <td>-18.72</td>\n",
       "      <td>36167.67</td>\n",
       "      <td>6</td>\n",
       "      <td>-5122.67</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>106 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     type symbol entry_date  exit_date   entry    exit  shares       pnl  \\\n",
       "id                                                                         \n",
       "1    long   TSLA 2018-01-03 2018-01-22   21.36   23.50    4679   9994.34   \n",
       "2    long   TSLA 2018-01-23 2018-03-27   23.72   19.20    4637 -20961.72   \n",
       "3    long   TSLA 2018-03-28 2018-04-04   17.36   19.10    4783   8303.29   \n",
       "4    long   TSLA 2018-04-05 2018-06-07   19.82   21.80    4910   9731.62   \n",
       "5    long   TSLA 2018-06-08 2018-06-12   21.39   23.53    5005  10705.70   \n",
       "..    ...    ...        ...        ...     ...     ...     ...       ...   \n",
       "102  long   TSLA 2022-08-02 2022-10-03  300.25  251.73    1095 -53125.76   \n",
       "103  long   TSLA 2022-10-04 2022-10-24  249.75  199.80    1104 -55144.80   \n",
       "104  long   TSLA 2022-10-25 2022-11-08  217.18  189.92    1015 -27668.90   \n",
       "105  long   TSLA 2022-11-09 2022-12-13  186.50  160.66    1008 -26050.75   \n",
       "106  long   TSLA 2022-12-14 2022-12-22  158.46  128.79    1036 -30736.04   \n",
       "\n",
       "     return_pct    agg_pnl  bars  pnl_per_bar      stop  \n",
       "id                                                       \n",
       "1         10.00    9994.34    12       832.86    profit  \n",
       "2        -19.06  -10967.37    44      -476.40  trailing  \n",
       "3         10.00   -2664.08     4      2075.82    profit  \n",
       "4         10.00    7067.54    44       221.17    profit  \n",
       "5         10.00   17773.23     2      5352.85    profit  \n",
       "..          ...        ...   ...          ...       ...  \n",
       "102      -16.16  175768.16    43     -1235.48  trailing  \n",
       "103      -20.00  120623.36    14     -3938.91  trailing  \n",
       "104      -12.55   92954.46    10     -2766.89  trailing  \n",
       "105      -13.86   66903.71    23     -1132.64  trailing  \n",
       "106      -18.72   36167.67     6     -5122.67  trailing  \n",
       "\n",
       "[106 rows x 13 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def buy_with_trailing_stop_loss_and_profit(ctx):\n",
    "    if not ctx.long_pos():\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)\n",
    "        ctx.stop_trailing_pct = 20\n",
    "        ctx.stop_profit_pct = 10\n",
    "        \n",
    "strategy.clear_executions()\n",
    "strategy.add_execution(buy_with_trailing_stop_loss_and_profit, ['TSLA'])\n",
    "result = strategy.backtest()\n",
    "result.trades"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1133e6e",
   "metadata": {},
   "source": [
    "## Setting a Limit Price\n",
    "\n",
    "A stop order can be combined with a limit price to ensure that the order is executed only at a specific price level. Below shows an example of placing a limit price on a stop order:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "65b7f914",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2018-01-01 00:00:00 to 2023-01-01 00:00:00\n",
      "\n",
      "Loaded cached bar data.\n",
      "\n",
      "Test split: 2018-01-02 00:00:00 to 2022-12-30 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:00\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2018-01-22</td>\n",
       "      <td>21.36</td>\n",
       "      <td>23.50</td>\n",
       "      <td>4679</td>\n",
       "      <td>9994.34</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9994.34</td>\n",
       "      <td>12</td>\n",
       "      <td>832.86</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-23</td>\n",
       "      <td>2019-12-18</td>\n",
       "      <td>23.72</td>\n",
       "      <td>26.09</td>\n",
       "      <td>4637</td>\n",
       "      <td>10998.96</td>\n",
       "      <td>10.0</td>\n",
       "      <td>20993.31</td>\n",
       "      <td>480</td>\n",
       "      <td>22.91</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2019-12-19</td>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>26.78</td>\n",
       "      <td>29.46</td>\n",
       "      <td>4518</td>\n",
       "      <td>12099.20</td>\n",
       "      <td>10.0</td>\n",
       "      <td>33092.51</td>\n",
       "      <td>9</td>\n",
       "      <td>1344.36</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2020-01-06</td>\n",
       "      <td>2020-01-08</td>\n",
       "      <td>29.72</td>\n",
       "      <td>32.69</td>\n",
       "      <td>4478</td>\n",
       "      <td>13308.62</td>\n",
       "      <td>10.0</td>\n",
       "      <td>46401.13</td>\n",
       "      <td>2</td>\n",
       "      <td>6654.31</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2020-01-09</td>\n",
       "      <td>2020-01-14</td>\n",
       "      <td>32.39</td>\n",
       "      <td>35.63</td>\n",
       "      <td>4462</td>\n",
       "      <td>14452.42</td>\n",
       "      <td>10.0</td>\n",
       "      <td>60853.55</td>\n",
       "      <td>3</td>\n",
       "      <td>4817.47</td>\n",
       "      <td>profit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    type symbol entry_date  exit_date  entry   exit  shares       pnl  \\\n",
       "id                                                                      \n",
       "1   long   TSLA 2018-01-03 2018-01-22  21.36  23.50    4679   9994.34   \n",
       "2   long   TSLA 2018-01-23 2019-12-18  23.72  26.09    4637  10998.96   \n",
       "3   long   TSLA 2019-12-19 2020-01-03  26.78  29.46    4518  12099.20   \n",
       "4   long   TSLA 2020-01-06 2020-01-08  29.72  32.69    4478  13308.62   \n",
       "5   long   TSLA 2020-01-09 2020-01-14  32.39  35.63    4462  14452.42   \n",
       "\n",
       "    return_pct   agg_pnl  bars  pnl_per_bar    stop  \n",
       "id                                                   \n",
       "1         10.0   9994.34    12       832.86  profit  \n",
       "2         10.0  20993.31   480        22.91  profit  \n",
       "3         10.0  33092.51     9      1344.36  profit  \n",
       "4         10.0  46401.13     2      6654.31  profit  \n",
       "5         10.0  60853.55     3      4817.47  profit  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def buy_with_trailing_stop_loss_and_profit(ctx):\n",
    "    if not ctx.long_pos():\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)\n",
    "        ctx.stop_trailing_pct = 20\n",
    "        ctx.stop_trailing_limit = ctx.close[-1] + 1\n",
    "        ctx.stop_profit_pct = 10\n",
    "        ctx.stop_profit_limit = ctx.close[-1] - 1\n",
    "        \n",
    "strategy.clear_executions()\n",
    "strategy.add_execution(buy_with_trailing_stop_loss_and_profit, ['TSLA'])\n",
    "result = strategy.backtest()\n",
    "result.trades.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88cf4aa8",
   "metadata": {},
   "source": [
    "## Canceling a Stop\n",
    "\n",
    "The following code shows an example of canceling a stop order:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1627ee6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2018-01-01 00:00:00 to 2023-01-01 00:00:00\n",
      "\n",
      "Loaded cached bar data.\n",
      "\n",
      "Test split: 2018-01-02 00:00:00 to 2022-12-30 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:00\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>21.36</td>\n",
       "      <td>19.23</td>\n",
       "      <td>4679</td>\n",
       "      <td>-9981.87</td>\n",
       "      <td>-9.99</td>\n",
       "      <td>-9981.87</td>\n",
       "      <td>57</td>\n",
       "      <td>-175.12</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    type symbol entry_date  exit_date  entry   exit  shares      pnl  \\\n",
       "id                                                                     \n",
       "1   long   TSLA 2018-01-03 2018-03-27  21.36  19.23    4679 -9981.87   \n",
       "\n",
       "    return_pct  agg_pnl  bars  pnl_per_bar      stop  \n",
       "id                                                    \n",
       "1        -9.99 -9981.87    57      -175.12  trailing  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def buy_with_stop_trailing_and_cancel(ctx):\n",
    "    pos = ctx.long_pos()\n",
    "    if not pos:\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)\n",
    "        ctx.stop_trailing_pct = 20\n",
    "    elif pos.bars > 60:\n",
    "        ctx.cancel_stops(ctx.symbol)\n",
    "        \n",
    "strategy.clear_executions()\n",
    "strategy.add_execution(buy_with_stop_trailing_and_cancel, ['TSLA'])\n",
    "result = strategy.backtest()\n",
    "result.trades"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91e5fe8c",
   "metadata": {},
   "source": [
    "## Setting the Stop Exit Price\n",
    "\n",
    "By default, stops are checked against the bar's low and high prices, and they are exited at the stop's threshold (e.g., -2%) on the same bar when the stop is triggered.\n",
    "\n",
    "To set a custom exit price, the \"exit_price\" fields for each stop type can be used. When these fields are set, the stop will be checked against the ``exit_price``, and it will exit at the ``exit_price`` when triggered. For example, the code below sets the [stop_trailing_exit_price](https://www.pybroker.com/en/dev/reference/pybroker.context.html#pybroker.context.ExecContext.stop_trailing_exit_price) to the open price on the bar that triggers the stop:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "23763dc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2018-01-01 00:00:00 to 2023-01-01 00:00:00\n",
      "\n",
      "Loaded cached bar data.\n",
      "\n",
      "Test split: 2018-01-02 00:00:00 to 2022-12-30 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:00\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>21.36</td>\n",
       "      <td>17.64</td>\n",
       "      <td>4679</td>\n",
       "      <td>-17412.12</td>\n",
       "      <td>-17.42</td>\n",
       "      <td>-17412.12</td>\n",
       "      <td>58</td>\n",
       "      <td>-300.21</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-03-29</td>\n",
       "      <td>2018-07-25</td>\n",
       "      <td>17.31</td>\n",
       "      <td>19.78</td>\n",
       "      <td>4771</td>\n",
       "      <td>11797.10</td>\n",
       "      <td>14.28</td>\n",
       "      <td>-5615.03</td>\n",
       "      <td>81</td>\n",
       "      <td>145.64</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-07-26</td>\n",
       "      <td>2018-08-20</td>\n",
       "      <td>20.48</td>\n",
       "      <td>19.45</td>\n",
       "      <td>4585</td>\n",
       "      <td>-4737.83</td>\n",
       "      <td>-5.05</td>\n",
       "      <td>-10352.86</td>\n",
       "      <td>17</td>\n",
       "      <td>-278.70</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-08-21</td>\n",
       "      <td>2018-09-07</td>\n",
       "      <td>21.13</td>\n",
       "      <td>17.34</td>\n",
       "      <td>4242</td>\n",
       "      <td>-16077.18</td>\n",
       "      <td>-17.94</td>\n",
       "      <td>-26430.04</td>\n",
       "      <td>12</td>\n",
       "      <td>-1339.76</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>long</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>2018-09-10</td>\n",
       "      <td>2018-12-26</td>\n",
       "      <td>18.57</td>\n",
       "      <td>20.00</td>\n",
       "      <td>3961</td>\n",
       "      <td>5664.23</td>\n",
       "      <td>7.70</td>\n",
       "      <td>-20765.81</td>\n",
       "      <td>74</td>\n",
       "      <td>76.54</td>\n",
       "      <td>trailing</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    type symbol entry_date  exit_date  entry   exit  shares       pnl  \\\n",
       "id                                                                      \n",
       "1   long   TSLA 2018-01-03 2018-03-28  21.36  17.64    4679 -17412.12   \n",
       "2   long   TSLA 2018-03-29 2018-07-25  17.31  19.78    4771  11797.10   \n",
       "3   long   TSLA 2018-07-26 2018-08-20  20.48  19.45    4585  -4737.83   \n",
       "4   long   TSLA 2018-08-21 2018-09-07  21.13  17.34    4242 -16077.18   \n",
       "5   long   TSLA 2018-09-10 2018-12-26  18.57  20.00    3961   5664.23   \n",
       "\n",
       "    return_pct   agg_pnl  bars  pnl_per_bar      stop  \n",
       "id                                                     \n",
       "1       -17.42 -17412.12    58      -300.21  trailing  \n",
       "2        14.28  -5615.03    81       145.64  trailing  \n",
       "3        -5.05 -10352.86    17      -278.70  trailing  \n",
       "4       -17.94 -26430.04    12     -1339.76  trailing  \n",
       "5         7.70 -20765.81    74        76.54  trailing  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pybroker import PriceType\n",
    "\n",
    "def buy_with_stop_trailing_and_exit_price(ctx):\n",
    "    pos = ctx.long_pos()\n",
    "    if not pos:\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)\n",
    "        ctx.stop_trailing_pct = 20\n",
    "        ctx.stop_trailing_exit_price = PriceType.OPEN\n",
    "        \n",
    "strategy.clear_executions()\n",
    "strategy.add_execution(buy_with_stop_trailing_and_exit_price, ['TSLA'])\n",
    "result = strategy.backtest()\n",
    "result.trades.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41c12b2e",
   "metadata": {},
   "source": [
    "[For more information on the various attributes that can be used to set stops in PyBroker, you can refer to the ExecContext reference documentation.](https://www.pybroker.com/en/latest/reference/pybroker.context.html#pybroker.context.ExecContext)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
